#ifndef BMI088DRIVER_H
#define BMI088DRIVER_H


// #include "main.h"
#include "stdint.h"
#define BMI088_USE_SPI
// #define BMI088_USE_IIC

#define BMI088_TEMP_FACTOR 0.125f
#define BMI088_TEMP_OFFSET 23.0f

#define TEMPERATURE_PID_MAX_IOUT 4400.0f // 温度控制PID的max_iout //4400

// 对照表
#define BMI088_WRITE_ACCEL_REG_NUM 6
#define BMI088_WRITE_GYRO_REG_NUM 6

#define BMI088_GYRO_DATA_READY_BIT 0
#define BMI088_ACCEL_DATA_READY_BIT 1
#define BMI088_ACCEL_TEMP_DATA_READY_BIT 2

#define BMI088_LONG_DELAY_TIME 80
#define BMI088_COM_WAIT_SENSOR_TIME 150

#define BMI088_ACCEL_IIC_ADDRESSE (0x18 << 1)
#define BMI088_GYRO_IIC_ADDRESSE (0x68 << 1)

#define BMI088_ACCEL_RANGE_3G
// #define BMI088_ACCEL_RANGE_6G
// #define BMI088_ACCEL_RANGE_12G
// #define BMI088_ACCEL_RANGE_24G

#define BMI088_GYRO_RANGE_2000
// #define BMI088_GYRO_RANGE_1000
// #define BMI088_GYRO_RANGE_500
// #define BMI088_GYRO_RANGE_250
// #define BMI088_GYRO_RANGE_125

#define BMI088_ACCEL_3G_SEN 0.0008974358974f
#define BMI088_ACCEL_6G_SEN 0.00179443359375f
#define BMI088_ACCEL_12G_SEN 0.003 588 867 1875f

#define BMI088_ACCEL_24G_SEN 0.000732421875f //! 24/32768

//! 弧度制 rad/s
#define BMI088_GYRO_2000_SEN 0.00106526443603169529841533860381f
#define BMI088_GYRO_1000_SEN 0.00053263221801584764920766930190693f
#define BMI088_GYRO_500_SEN 0.00026631610900792382460383465095346f
#define BMI088_GYRO_250_SEN 0.00013315805450396191230191732547673f
#define BMI088_GYRO_125_SEN 0.000066579027251980956150958662738366f
//!角度 摄氏度/s
#define  BMI088_GYRO_SEN_angle  0.061203256013f



enum {
    BMI088_NO_ERROR = 0x00,
    BMI088_ACC_PWR_CTRL_ERROR = 0x01,
    BMI088_ACC_PWR_CONF_ERROR = 0x02,
    BMI088_ACC_CONF_ERROR = 0x03,
    BMI088_ACC_SELF_TEST_ERROR = 0x04,
    BMI088_ACC_RANGE_ERROR = 0x05,
    BMI088_INT1_IO_CTRL_ERROR = 0x06,
    BMI088_INT_MAP_DATA_ERROR = 0x07,
    BMI088_GYRO_RANGE_ERROR = 0x08,
    BMI088_GYRO_BANDWIDTH_ERROR = 0x09,
    BMI088_GYRO_LPM1_ERROR = 0x0A,
    BMI088_GYRO_CTRL_ERROR = 0x0B,
    BMI088_GYRO_INT3_INT4_IO_CONF_ERROR = 0x0C,
    BMI088_GYRO_INT3_INT4_IO_MAP_ERROR = 0x0D,

    BMI088_SELF_TEST_ACCEL_ERROR = 0x80,
    BMI088_SELF_TEST_GYRO_ERROR = 0x40,
    BMI088_NO_SENSOR = 0xFF,
};


//! 默认只初始化与检验一遍 后续可增加独立性检验部分
uint8_t BMI088_init(void);

uint8_t bmi088_accel_init(void);

uint8_t bmi088_gyro_init(void);

void BMI088_readaccel(float accel[3]);

void BMI088_readgyro(float gyro[3]);

void get_BMI088_gyro(float gyro[3]);

void get_BMI088_accel(float accel[3]);

void BMI088_write_single_reg(uint8_t reg, uint8_t data);

void BMI088_read_single_reg(uint8_t reg, uint8_t *return_data);

void BMI088_read_muli_reg(uint8_t reg, uint8_t *buf, uint8_t len);

/**
 * @brief 根据读取到的BMI088C传感器温度数据,与定时器确定的固定时间,进行温度控制
 * @note 温度控制模块移植到驱动外部,因为要保证初始化控制温度,削减温度带来的零漂(还有一种方案就是直接估计漂移比例然后直接
 * 等到那个温度再减掉
 *
 *
 */
float get_BMI088_temperate(void);

uint32_t get_BMI088_sensor_time(void);

void BMI088_delay_ms(uint16_t ms);

void BMI088_delay_us(uint16_t us);

/**
 * @source //!jxust_c板BSP_delay.c
 * @brief
    //!官方原先专门做了一个BSP层来提供延时,但实际上只要使用FREERTOS
    //!及以下方法便可以实现微秒毫秒级延时
 *
 * @param nus
 */
void delay_us(uint16_t nus);

#if defined(BMI088_USE_SPI)

void BMI088_ACCEL_NS_L(void);

void BMI088_ACCEL_NS_H(void);

void BMI088_GYRO_NS_L(void);

void BMI088_GYRO_NS_H(void);

uint8_t BMI088_read_write_byte(uint8_t reg);

#elif defined(BMI088_USE_IIC)

#endif

#endif
